package com.cart.test;

/**
 * Created by zhaoliang on 2017/3/29.
 */

import com.cart.test.util.HttpUtils;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cart.test.TestChangeImage.GetImageStr;

public class Xingshizheng {
    public static void main(String[] args) {
        String host = "https://dm-53.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_vehicle.json";
        String method = "POST";
        String appcode = "你的appCode";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");

        String strImg = GetImageStr();
        System.out.println("base64格式的图片内容---："+strImg);
        System.out.println("结束");

        Gson gosn = new Gson();

        List<Map> array = new ArrayList<Map>();
        Map<String, Object> imageMap = new HashMap<String, Object>();
        imageMap.put("dataType", 50);
        imageMap.put("dataValue", strImg);

        Map<String, Object> arrayMap = new HashMap<String, Object>();
        arrayMap.put("image", imageMap);
        array.add(arrayMap);

        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("inputs", array);


        String img=gosn.toJson(jsonMap);
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = img;
     //   String bodys = "{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\"Base64编码的字符\"}}]}";

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
